<script type="text/javascript">
$(function(){
	var pg = App.message.recipientIndex.ui, pp = pg.params || {},
		catalogLb = pg.children('.head').seek('title');

	var gd = pg.find('.grid').mac('grid', {
		key: 'id',
		cols: [{
			field: 'catalog', title : '所属系统', width: 100,
			render: function(r, tr, me){
				return $('<span class="a"></span>').click(function(){
					me.load({ catalog: r.catalog, pageNo: 1, key: '' });
				}).append(r.catalog);
			}
		}, {
			field: 'code', title : '编号', width: 100,
		}, {
			field: 'name', title : '名称 ', width: 140
// 		}, {
// 			field: 'type', title : '类型 ', width: 45,
// 			render: function(r) {
// 				return Cache.message.queue.type[r.type];
// 			}
// 		}, {
// 			field: 'capacity', title : '队列长度 ', width: 80
// 		}, {
// 			field: 'priority', title : '优先级', width: 24
// 		}, {
// 			field: 'reliability', title : '可靠性', width: 24
// 		}, {
// 			field: 'term', title : '消息有效期', width: 40
		},{
			field: 'status', title : '状态', width: 80,
			render: function(r) {
				return Cache.message.queue.status[r.status];
			}
		},{
			field: 'monitor', title : '监控', width: 80,
			render: function(r) {
				if(r.status == 1){
					return $('<span class="a">开启</span>').click(function(){
						mac.confirm('确定要开启 队列 ' + r.code + ' 的监控吗？', function(){
							$.post(App.ctx + '/recipient/queue/start.do', {
								code: r.code,
								id:r.id,
							}, function(rs){
								var ro = mac.eval(rs);
								if (ro.success) {
									gd.load();
								} else {
									mac.alert(ro.data, ro.params);
								}
							});
						});
					});
				}
				if(r.status == 2){
					var p = $('<div></div>'), st = '<span class="a"></span>';
					$(st).click(function(){
					mac.confirm('确定要停止队列 ' + r.code + ' 的监控吗？', function(){
						$.post(App.ctx + '/recipient/queue/stop.do', {
							code: r.code,
							id:r.id,
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								gd.load();
							} else {
								mac.alert(ro.data, ro.params);
							}
						});
						});
					}).append('停止').appendTo(p);
					p.append('&nbsp;');
					$(st).click(function(){
						App.show({
							title: '队列监控',
							ctx: 'message',
							action: 'recipientMonitorIndex',
							params: r
						}, $.noop, pg.parent());
					}).append('查看').appendTo(p);
					return p;
				}
			}
		},{
			field: 'id', title : '操作', width: 100,
			render: function(r, tr, gd) {
				var p = $('<div></div>'), v = r.status,
					aa = ['enable', 'disable'], la = ['启用','停用'],
					st = '<span class="a"></span>';
				$(st).click(function(){
					mac.confirm('确定要 ' + la[v] + ' 队列 ' + r.code + ' 吗？', function(){
						$.post(App.ctx + '/recipient/queue/' + aa[r.status] + '.do', {
							id: r.id
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								gd.load();
							} else {
								mac.alert(ro.data, ro.params);
							}
						});
					});
				}).append(la[v]).appendTo(p);
				p.append('&nbsp;');
				if(v == 0){
					$(st).click(function(){
						mac.confirm('确定要 删除 队列 ' + r.code + ' 吗？', function(){
							$.post(App.ctx + '/recipient/queue/remove.do', {
								id: r.id
							}, function(rs){
								var ro = mac.eval(rs);
								if (ro.success) {
									gd.load();
								} else {
									mac.alert(ro.data, ro.params);
								}
							});
						});
					}).append('删除').appendTo(p);
				}
				return p;
			}
		}],
		loader: {
			url: App.ctx + '/recipient/queue/list.do',
			params: {
				pageNo: 1, pageSize: 20
			},
			autoLoad: true
		},
		pagerLength: 20
	});
	pg.seek('back').click(function(){
		catalogLb.text('全部收件人与主题');
		gd.load({ catalog: '', pageNo: 1, key: '' });
	});
	if (App.logon.power('message.recipient.queue.save')) {
		var p = pg.parent();
		pg.seek('addRecipient').click(function(){
			App.show({
				title: '添加收件人',
				ctx: 'message',
				action: 'recipientEdit',
				params: {}
			}, $.noop, p).show();
		}).show();
	}
	function adjust(){
		var h = pg.parent().height();
		pg.height(h);
		gd.parent().height(h - 82);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="navi">
	<span class="a" name="back">收件人管理</span>&gt;&gt;
	<span name="title">已知收件人</span>
</div>
<div class="head">
	<input type="text" name="key" class="fr" placeholder="查找" />
	<button name="addRecipient" class="hidden">添加收件人</button>
</div>
<div class="body">
	<div class="grid"></div>
</div>